import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

/* Layout */
import Layout from '@/layout'

/**
 * Note: sub-menu only appear when route children.length >= 1
 * Detail see: https://panjiachen.github.io/vue-element-admin-site/guide/essentials/router-and-nav.html
 *
 * hidden: true                   if set true, item will not show in the sidebar(default is false)
 * alwaysShow: true               if set true, will always show the root menu
 *                                if not set alwaysShow, when item has more than one children route,
 *                                it will becomes nested mode, otherwise not show the root menu
 * redirect: noRedirect           if set noRedirect will no redirect in the breadcrumb
 * name:'router-name'             the name is used by <keep-alive> (must set!!!)
 * meta : {
    roles: ['admin','editor']    control the page roles (you can set multiple roles)
    title: 'title'               the name show in sidebar and breadcrumb (recommend set)
    icon: 'svg-name'/'el-icon-x' the icon show in the sidebar
    breadcrumb: false            if set false, the item will hidden in breadcrumb(default is true)
    activeMenu: '/example/list'  if set path, the sidebar will highlight the path you set
  }
 */

/**
 * constantRoutes
 * a base page that does not have permission requirements
 * all roles can be accessed
 */
export const asyncRoutes = []
export const constantRoutes = [
  {
    path: '/',
   redirect: "/login"
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },

  {
    path: '/404',
    component: () => import('@/views/404'),
    hidden: true
  },
  {
    path: "/page",
    component: Layout,
    component: () => import('@/views/page/index'),
    children: [
      {
        path: '/routerChildren',  //代表 /routerChildren
        redirect: '/page/library/cont' //当我们访问存在子路由的页面时，重定向为第一个子路由，切记，如果写了component，那么这个component也将显示
      },
      {
        path: '/page/library',
        name: '电子图书馆',
        component: Layout,
        component: () => import('@/views/page/library'),
        children: [
          {
            path: '/page/library/cont',
            name: 'librayIndex',
            component: () => import('@/components/library/cont'),
          },
          {
            path: '/page/library/recommend',
            name: '推荐图书',
            component: () => import('@/components/library/recommend'),
          },
          {
            path: '/page/library/hot',
            name: '热门图书',
            component: () => import('@/components/library/hot'),
          },
          {
            path: '/page/library/new',
            name: '最新图书',
            component: () => import('@/components/library/new'),
          },
          {
            path: '/page/library/reading',
            name: '图书馆阅读',
            component: () => import('@/components/library/reading'),
          }
        ]
      },
      {
        path: '/page/av',
        name: '电子音像馆',
        component: Layout,
        component: () => import('@/views/page/av'),
        children: [
          {
            path: '/page/av/cont',
            name: 'avIndex',
            component: () => import('@/components/av/cont'),
          },
          {
            path: '/page/av/recommend',
            name: '推荐音像',
            component: () => import('@/components/av/recommend'),
          },
          {
            path: '/page/av/hot',
            name: '热门音像',
            component: () => import('@/components/av/hot'),
          },
          {
            path: '/page/av/new',
            name: '最新音像',
            component: () => import('@/components/av/new'),
          },
          {
            path: '/page/av/reading',
            name: '音像馆阅读',
            component: () => import('@/components/av/reading'),
          }
        ]
      },
      {
        path: '/page/center',
        name: '个人中心',
        component: Layout,
        component: () => import('@/views/page/center'),
        children: [
          {
            path: '/page/center/cont',
            name: 'centerIndex',
            component: () => import('@/components/center/cont'),
          },
          {
            path: '/page/center/recommend',
            name: '我的收藏',
            component: () => import('@/components/center/recommend'),
          },
          {
            path: '/page/center/hotNew',
            name: '我的下载',
            component: () => import('@/components/center/hotNew'),
          },
          {
            path: '/page/center/history',
            name: '浏览历史',
            component: () => import('@/components/center/history'),
          },
          {
            path: '/page/center/reading',
            name: '个人中心阅读',
            component: () => import('@/components/center/reading'),
          }
        ]
      },
      // {
      //   path:'/page/news',
      //   name:'新闻中心',
      //   component: () => import('@/views/page/news'),
      // }
    ]
  },

  // {
  //   path: '/',
  //   component: Layout,
  //   redirect: '/dashboard',
  //   children: [{
  //     path: 'dashboard',
  //     name: '首页',
  //     component: () => import('@/views/dashboard/index'),
  //     meta: { title: '数字浏览室后台系统', icon: 'dashboard' }
  //   }]
  // },
  {
    path: '/library',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Library',
        component: () => import('@/views/Library/index'),
        meta: { title: '图书管理', icon: 'table' }
      }
    ]
  },
  {
    path: '/audioVisual',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'AudioVisual',
        component: () => import('@/views/audioVisual/index'),
        meta: { title: '音像管理', icon: 'table' }
      }
    ]
  },
  {
    path: '/notice',
    component: Layout,
    children: [
      {
        path: 'index',
        name: 'Notice',
        component: () => import('@/views/notice/index'),
        meta: { title: '通知管理', icon: 'table' }
      }
    ]
  },

  {
    path: '/classification',
    component: Layout,
    redirect: '/classification/book',
    name: 'Classification',
    meta: { title: '分类管理', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'book',
        name: 'Book',
        component: () => import('@/views/classification/book'),
        meta: { title: '图书分类', icon: 'table' }
      },
      {
        path: 'audioVideo',
        name: 'AudioVideo',
        component: () => import('@/views/classification/audioVideo'),
        meta: { title: '音像分类', icon: 'tree' }
      }
    ]
  },
  {
    path: '/statisticalAnalysis',
    component: Layout,
    redirect: '/statisticalAnalysis/reading',
    name: 'StatisticalAnalysis',
    meta: { title: '统计分析', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'reading',
        name: 'Reading',
        component: () => import('@/views/statisticalAnalysis/reading'),
        meta: { title: '阅读量', icon: 'table' }
      },
      {
        path: 'downloads',
        name: 'Downloads',
        component: () => import('@/views/statisticalAnalysis/downloads'),
        meta: { title: '下载量', icon: 'tree' }
      },
      {
        path: 'collection',
        name: 'Collection',
        component: () => import('@/views/statisticalAnalysis/collection'),
        meta: { title: '收藏量', icon: 'tree' }
      }
    ]
  },
  {
    path: '/systemManagement',
    component: Layout,
    redirect: '/systemManagement/user',
    name: 'systemManagement',
    meta: { title: '设置', icon: 'el-icon-s-help' },
    children: [
      {
        path: 'user',
        name: 'User',
        component: () => import('@/views/systemManagement/user'),
        meta: { title: '用户设置', icon: 'table' }
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('@/views/systemManagement/role'),
        meta: { title: '角色设置', icon: 'table' }
      },
      {
        path: 'permissions',
        name: 'Permissions',
        component: () => import('@/views/systemManagement/permissions'),
        meta: { title: '权限设置', icon: 'table' }
      },
      {
        path: 'school',
        name: 'School',
        component: () => import('@/views/systemManagement/school'),
        meta: { title: '学校设置', icon: 'table' }
      },
      {
        path: 'grade',
        name: 'Grade',
        component: () => import('@/views/systemManagement/grade'),
        meta: { title: '年级设置', icon: 'table' }
      },
      {
        path: 'class',
        name: 'Class',
        component: () => import('@/views/systemManagement/class'),
        meta: { title: '班级设置', icon: 'table' }
      },
      {
        path: 'keyword',
        name: 'Keyword',
        component: () => import('@/views/systemManagement/keyword'),
        meta: { title: '关键字设置', icon: 'table' }
      }
    ]
  },


  // 404 page must be placed at the end !!!
  // { path: '*', redirect: '/404', hidden: true }
]

const createRouter = () => new Router({
  // mode: 'history', // require service support
  scrollBehavior: () => ({ y: 0 }),
  base: process.env.BASE_URL,
  routes: constantRoutes
})

const router = createRouter()

export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}
router.selfaddRoutes = function (params) {
  router.matcher = new Router().matcher;
  router.addRoutes(params)
}

export default router
